package adminDynamicEnumService

import (
	"encoding/json"

	"gitee.com/krio/ginc/example/core/exerr"
	exampleDtoApiPbAdminDynamicEnum "gitee.com/krio/ginc/example/dto/api/pb/admin/dynamic/enum"
	adminDynamicEnumDao "gitee.com/krio/ginc/example/internal/admin/dynamic/enum/dao"
	adminDynamicEnumModel "gitee.com/krio/ginc/example/internal/admin/dynamic/enum/model"
	commonEntry "gitee.com/krio/ginc/example/internal/common/entry/common"
	commonModel "gitee.com/krio/ginc/example/internal/common/model/common"
	"github.com/gin-gonic/gin"
)

// DynamicEnumValueList 列表
func DynamicEnumValueList(ctx *gin.Context, req *exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueListReq) (*exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueListResp, error) {
	resp := &exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueListResp{}
	search := struct {
		Keyword string `json:"keyword"`
	}{}
	if req.Keywords != "" {
		err := json.Unmarshal([]byte(req.Keywords), &search)
		if err != nil {
			return nil, err
		}
	}
	dao := &adminDynamicEnumDao.DynamicEnumValueDao{}
	list, total, err := dao.DynamicEnumValueList(ctx, &commonEntry.ListCommonReq{
		Page:    req.Page,
		Limit:   req.Limit,
		Keyword: search.Keyword,
		OrderBy: []*commonEntry.OrderBy{
			{
				IsDesc: true,
				Field:  "id",
			},
		},
	})
	if err != nil {
		return nil, err
	}
	var data []*exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueInfo
	for _, v := range list {
		data = append(data, formatSubDetail(v))
	}
	resp.Total = total
	resp.List = data
	return resp, nil
}

// DynamicEnumValueDetail 详情
func DynamicEnumValueDetail(ctx *gin.Context, req *exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueDetailReq) (*exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueInfo, error) {
	dao := &adminDynamicEnumDao.DynamicEnumValueDao{}
	detail, has, err := dao.DynamicEnumValueDetail(ctx, req.Id)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, exerr.DataNotFound
	}

	return formatSubDetail(detail), nil
}

// DynamicEnumValueAdd 新增
func DynamicEnumValueAdd(ctx *gin.Context, req *exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueInfo) (*exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueAddResp, error) {
	one := &adminDynamicEnumModel.DynamicEnumValue{
		Basic:     commonModel.Basic{},
		EnumKey:   req.EnumKey,
		ValueName: req.ValueName,
		CharValue: req.CharValue,
		CodeValue: req.CodeValue,
		AfterId:   req.AfterId,
		Status:    commonModel.DataStatus(req.Status),
	}
	dao := &adminDynamicEnumDao.DynamicEnumValueDao{}
	id, err := dao.DynamicEnumValueAdd(ctx, one)
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueAddResp{
		Id: id,
	}, nil
}

// DynamicEnumValueUpdate 修改
func DynamicEnumValueUpdate(ctx *gin.Context, req *exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueInfo) (*exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueAddResp, error) {
	dao := &adminDynamicEnumDao.DynamicEnumValueDao{}
	_, err := dao.DynamicEnumValueUpdate(ctx, &adminDynamicEnumModel.DynamicEnumValue{
		Basic:     commonModel.Basic{ID: req.Id},
		EnumKey:   req.EnumKey,
		ValueName: req.ValueName,
		CharValue: req.CharValue,
		CodeValue: req.CodeValue,
		AfterId:   req.AfterId,
		Status:    commonModel.DataStatus(req.Status),
	})
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueAddResp{}, nil
}

// DynamicEnumValueDelete 删除
func DynamicEnumValueDelete(ctx *gin.Context, req *exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueDeleteReq) (*exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueDeleteResp, error) {
	dao := &adminDynamicEnumDao.DynamicEnumValueDao{}
	err := dao.DynamicEnumValueDelete(ctx, req.Ids)
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueDeleteResp{}, nil
}

// 数据装换
func formatSubDetail(v *adminDynamicEnumModel.DynamicEnumValue) *exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueInfo {
	return &exampleDtoApiPbAdminDynamicEnum.DynamicEnumValueInfo{
		Id:         v.ID,
		EnumKey:    v.EnumKey,
		ValueName:  v.ValueName,
		CharValue:  v.CharValue,
		CodeValue:  v.CodeValue,
		AfterId:    v.AfterId,
		Status:     int32(v.Status),
		CreateTime: v.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdateTime: v.UpdatedAt.Format("2006-01-02 15:04:05"),
	}
}
